package com.xd.spring.liteflow.component;

import com.xd.spring.liteflow.entity.UserInfo;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.stereotype.Component;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Component("a")
@Slf4j
public class ACmp extends NodeComponent {
    @Override
    public void process() throws Exception {
        String name = Thread.currentThread().getName();
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        StackTraceElement stackTraceElement = stackTraceElements[1];
        String className = stackTraceElement.getClassName();
        String methodName = stackTraceElement.getMethodName();
        int lineNumber = stackTraceElement.getLineNumber();
        Integer loopIndex = this.getLoopIndex();
        Object requestData = this.getRequestData();
        log.info("{} {} {} {} {} {} {} begin", name, className, methodName, lineNumber, System.currentTimeMillis(), loopIndex, requestData);
        TimeUnit.SECONDS.sleep(1);

        UserInfo userInfo = this.getContextBean(UserInfo.class);
        log.info("{}", ToStringBuilder.reflectionToString(userInfo, ToStringStyle.JSON_STYLE));
        if (Objects.nonNull(userInfo)) {
            if (Objects.nonNull(requestData)) {
                if (requestData instanceof Long) {
                    Long id = (Long) requestData;
                    if (Objects.isNull(userInfo.getId())) {
                        userInfo.setId(id);
                    }
                }
            }
        }
        log.info("{}", ToStringBuilder.reflectionToString(userInfo, ToStringStyle.JSON_STYLE));

        log.info("{} {} {} {} {} {} {} end", name, className, methodName, lineNumber, System.currentTimeMillis(), loopIndex, requestData);
    }


}
